﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ViewModels.FileVM;
using WebLib.FileUpload;

namespace HinokyWeb.Controllers
{
    public class TestController : Controller
    {
        public ActionResult ImageUpload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ImageUpload(FormCollection form)
        {

            //HttpPostedFile file = Server.MapPath("~/uploads/");
            //file.SaveAs("aa.jpg");

            //if (Request.Files.Count > 0)
            //{
            //    HttpFileCollectionBase files = Request.Files;
            //    foreach (string key in files)
            //    {
            //        HttpPostedFile file = files[key];
            //        string fileName = file.FileName;
            //        fileName = context.Server.MapPath("~/uploads/" + fileName);
            //        file.SaveAs(fileName);
            //    }
            //}
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("File(s) uploaded successfully!");


            return View();
        }

        [HttpGet]
        public ActionResult ImageUpload2()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ImageUpload2(IEnumerable<HttpPostedFileBase> files)
        {
            UploadFile(files.FirstOrDefault());

            return Json("All files have been successfully stored.");
        }

        [HttpGet]
        public ActionResult ImageUpload3()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ImageUpload3(IEnumerable<ImageDataVM> images)
        {
            // [참고자료] http://stackoverflow.com/questions/4986679/using-sendasbinary-with-asp-net-mvc 

            foreach (var item in images ?? Enumerable.Empty<ImageDataVM>())
            {
                var tokens = item.Image.Split(',');
                if (tokens.Length > 1)
                {
                    var buffer = Convert.FromBase64String(tokens[1]);
                    var file = string.Format(@"D:\Test\{0}", item.Filename);
                    System.IO.File.WriteAllBytes(file, buffer);
                }
            }

            return Json(new {Status = "OK"});
        }

        [HttpGet]
        public ActionResult ImageUpload4()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ImageUpload4(IEnumerable<ImageDataVM> images)
        {
            // [참고자료] http://stackoverflow.com/questions/4986679/using-sendasbinary-with-asp-net-mvc 

            foreach (var item in images ?? Enumerable.Empty<ImageDataVM>())
            {
                var tokens = item.Image.Split(',');
                if (tokens.Length > 1)
                {
                    //var buffer = Convert.FromBase64String(tokens[1]);
                    //var file = string.Format(@"D:\Test\{0}", item.Filename);
                    //System.IO.File.WriteAllBytes(file, buffer);
                    var aa = UploadHelper.UploadImageFile(Convert.FromBase64String(tokens[1]), item.Filename);


                }
            }

            return Json(new { Status = "OK" });
        }

        [HttpGet]
        public ActionResult RequestAnimationFrame()
        {
            return View();
        }







        public void UploadFile(HttpPostedFileBase file)
        {
            //System.IO.File.WriteAllBytes(Server.MapPath("~/Content/upload/" + file.FileName), ReadData(file.InputStream));
            System.IO.File.WriteAllBytes(string.Format(@"D:\Test\{0}", file.FileName), ReadData(file.InputStream));
        }

        private byte[] ReadData(Stream stream)
        {
            var buffer = new byte[16 * 1024];

            using (var ms = new MemoryStream())
            {
                int read;
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }

                return ms.ToArray();
            }
        }
    }
}
